package com.tc.gateway.device.remote;

import java.util.ArrayList;
import java.util.Collection;


import com.byq.mm.abstractx.cluster.server.executor.Executor;
import com.byq.mm.abstractx.device.Cluster;
import com.byq.mm.application.cluster.provider.ObdProvider;
import com.byq.mm.application.cluster.provider.OnoffProvider;
import com.byq.mm.application.cluster.provider.TunnelProvider;
import com.byq.mm.application.cluster.server.ObdServer;
import com.byq.mm.application.cluster.server.OnoffServer;
import com.byq.mm.application.cluster.server.TunnelServer;
import com.byq.mm.application.cluster.server.executor.ObdExecutor;
import com.byq.mm.application.cluster.server.executor.OnoffExecutor;
import com.byq.mm.foundation.cluster.datatype.ByteArrayValue;
import com.byq.mm.foundation.cluster.server.BasicClusterServer;
import com.tc.gateway.device.listener.ObdIIListener;
import com.tc.gateway.device.listener.SimpleSensorListener;
import com.tc.gateway.device.type.ObdIIDevice;
import com.tc.gateway.device.type.SimpleSensorDevice;

public class ObdIIController extends RemoteController<ObdIIDevice> implements ObdIIListener{
	
	public ObdIIController(ObdIIDevice device) {
		super(device);
	}

	ObdServer server;
	OnoffServer onoffserver;
	TunnelServer tunnelServer;
	public Executor getExecutor(BasicClusterServer s) {
		if(s instanceof ObdServer){
		server = (ObdServer)s; 
		
		return new ObdExecutor(){

			@Override
			public void onRequest(String req) {
				String resp = device.clientRequest(req);
				server.response(req, resp);
				
			}
		};
		}
		if(s instanceof OnoffServer){
			onoffserver = (OnoffServer)s;
			return new OnoffExecutor(){

				@Override
				public void off() {
				
					
				}

				@Override
				public void on() {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void toggle() {
					boolean onoff = onoffserver.getOnOff().isValue();
					device.clientOnOff(onoff);
					
				}
				
			};
		}
		if(s instanceof TunnelServer){
			tunnelServer = (TunnelServer)s;
			return new Executor(){
				
			};
		}
		return null;
		
	}



	@Override
	protected Collection<Cluster> getSupportedClusters() {
		Collection<Cluster> rst = new ArrayList<Cluster>();
		Cluster c = new Cluster();
		c.setId(ObdProvider.Define.CLUSTER_NAME);
		rst.add(c);
		
		c = new Cluster();
		c.setId(OnoffProvider.Define.CLUSTER_NAME);
		rst.add(c);
		
		
		c = new Cluster();
		c.setId(TunnelProvider.Define.CLUSTER_NAME);
		rst.add(c);
		return rst;
	}



	@Override
	public void onResponse(String req, String resp) {
		server.response(req, resp);
		
	}



	@Override
	public void onReport(byte[] data) {
		tunnelServer.onValueChange(new ByteArrayValue(data));
		
	}



	@Override
	public void onChange(boolean onoff){
		this.onoffserver.onChange(onoff);
		
	}

	
}
